import com.sun.scenario.effect.impl.sw.java.JSWColorAdjustPeer;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:快速排序 -- 挖坑法
 *时间复杂度O(nlogn)
 * 空间复杂度O(logn)
 * 稳定性: 不稳定
 * User: 86183
 * Date: 2023-05-05
 * Time: 15:17
 */
public class sortDeom8 {
    public static void main(String[] args) {
        int[] a = {34,67,12,89,34,14,56,23};
        quickSort(a);
        System.out.println(Arrays.toString(a));
    }

    private static void quickSort(int[] a) {
        quick(a,0,a.length-1);
    }

    private static void quick(int[] a, int start, int end) {
        if(start >= end){
            return;
        }
        int povoit = partition(a,start,end);
        quick(a,start,povoit -1);
        quick(a,povoit+1,end);
    }

    private static int partition(int[] a, int start, int end) {
        int i = start;
        int poviot = a[start];
        while (start < end){
            while (start < end && a[end] >= poviot){
                end--;
            }
            a[start] = a[end];
            while (start < end && a[start] <= poviot){
                start++;
            }
           a[end] = a[start];

        }
        a[start] = poviot;
        return start;
    }

    private static void swap(int[] a, int start, int end) {
        int tmp = a[start];
        a[start] = a[end];
        a[end] = tmp;
    }
}
